import React from 'react';
import { obj_str, for_obj, uDb, reload_col, loc, d_sort, cline } from 'sui';

const code_desc = (data) => {
    let desc = obj_str(data, "desc");
    desc = desc.split(",");

    const ret = [];

    desc.forEach((d, i) => {
        let t = d.split(":");
        if (t.length === 1) {
            ret.push(<div key={`k${i}`} className="font-bold">{t[0]}</div>);
        } else if (t.length === 2) {
            ret.push(<div key={`k${i}`} className="mx-4">{t[0]}<span className="ml-2 bg-blue-200 px-2 rounded-lg">{t[1]}</span></div>);
        }
    });

    let std = obj_str(data, "std");
    if (std) {
        if (std === "inner") {
            std = loc("l_808");
        } else if (std === "outer") {
            std = loc("l_809");
        }

        ret.push(<div key="std" className="font-bold">{std}</div>);
    }

    const face = obj_str(data, "face");
    if (face) {
        ret.push(<div key="face" className="font-bold">{`${face}${loc("l_814")}`}</div>);
    }

    data["desc"] = (
        <div className="inline-flex">
            {ret}
        </div>
    );
};

const load_sale = (hDone, setMain) => {
    uDb(hDone, "buy/list", { line: cline() },
        r => {
            d_sort(r, "sn");

            r.forEach((d, i) => {
                d["index"] = i + 1;
                code_desc(d);
            });

            setMain(r);
        },
        e => setMain(null));
};

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_518")},
    ];
};

const load_col = (setCol) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), "buy", fix);
};

const load_supply = (setMain, mapSupply, tp="supply", gtp="group") => {
    uDb(null, `file/list/${tp}`, { line: cline() },
        supply => {
            const map = {};
            supply.forEach(d => map[d.sn] = d.name);

            mapSupply(map);

            if (setMain) {
                uDb(null, `file/list/${gtp}`, { line: cline() }, group => setMain(group), e => setMain(null));
            }
        },
        e => {
            mapSupply({});

            if (setMain) {
                setMain(null);
            }
        });
};

const kind_name = () => {
    return {
        cage: loc("l_528"),
        seal: loc("l_529"),
        forge: loc("l_893"),
        lathe: loc("l_894"),
        cera: loc("l_713"),
        stell: loc("l_712"),
        oil: loc("l_116"),
        part: loc("l_137"),
        in: loc("l_566"),
        back: loc("l_767"),
        fin: loc("l_526"),
    };
};

const load_kind = (setMain, mapStuff) => load_supply(setMain, v => {
    let tmp = kind_name();

    for_obj(v, (k, v) => tmp[k] = v);

    mapStuff(tmp);
}, "kind", "gkind");

const load_stuff = (setMain, mapStuff) => load_supply(setMain, mapStuff, "stuff", "gstuff");

const show_val = (props, key, data, ek) => {
    const d = props.row;

    const k = obj_str(d, key);

    if (!data) {
        return k;
    }

    if (ek) {
        const v = obj_str(data[k], ek);
        if (v) {
            return v;
        }
    } else {
        const v = obj_str(data, k);
        if (v) {
            return v;
        }
    }

    return k;
};

export {code_desc, show_val, load_stuff, kind_name, load_supply, load_kind, load_col, load_sale};
